package com.sky.controller.admin;

import com.sky.dto.CategoryAddDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.dto.EmployeeDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/category")
@Slf4j
@Api(tags = "分类相关接口")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public Result<PageResult> querycategoryPage(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("分页查询：{}", categoryPageQueryDTO);
        PageResult pageResult = categoryService.querycategoryPage(categoryPageQueryDTO);
        return Result.success(pageResult);
    }
    @PostMapping
    @ApiOperation(value = "新增分类")
    public Result addCategory(@RequestBody() @Valid CategoryAddDTO CategoryAddDTO){
        log.info("员工登录：{}", CategoryAddDTO);
        categoryService.addCategory(CategoryAddDTO);
        return Result.success();
    }
    @PostMapping("status/{status}")
    @ApiOperation(value = "根据id修改分类状态")
    public Result updateCategoryStatus(@PathVariable Integer status, @RequestParam Long id){
        categoryService.updateCategoryStatus(status, id);
        return Result.success();
    }
    @DeleteMapping
    @ApiOperation(value = "根据id删除分类")
    public Result deleteCategory(Long id){
        log.info("根据id删除分类：{}", id);
        categoryService.deleteCategory(id);
        return Result.success();
    }
    @PutMapping
    @ApiOperation(value = "根据id修改分类")
    public Result updateCategory(@RequestBody @Valid CategoryAddDTO categoryAddDTO){
        log.info("根据id修改分类：{}", categoryAddDTO);
        categoryService.updateCategory(categoryAddDTO);
        return Result.success();
    }
    @GetMapping("/list")
    @ApiOperation(value = "根据类型查询分类")
    public Result<List<Category>> queryCategoryByType(@RequestParam Integer type){
        List<Category> list = categoryService.queryCategoryByType(type);
        return Result.success(list);
    }
    private void cleanCache(String pattern) {
        //清理所有缓存数据
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
